<?php

class Banner extends MY_Admin
{
	// Protected or private properties
	protected $_template;
	
	// Constructor
	public function __construct()
	{
		parent::__construct();

		// Check if the logged user is an administrator
		//$this->access_library->check_access();

		// Load needed models, libraries, helpers and language files
		$this->load->module_model('admin', 'banner_model', 'banner');
		$this->load->module_language('admin', 'general');
		$this->load->module_language('admin', 'banner');
		
	}
	
	public function index()
	{
		$data['title']		=	'Danh sách Banner';
		$lang_working 		= load_language_working();
		$data['banner'] 	= $this->banner->get_all_banner();
		$this->_template['page']	= "website/banner/list";
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	public function add()
	{
		$lang_working = load_language_working();
		$this->form_validation->set_rules('name', 'lang:form_name', 'required|max_length[60]|xss_clean');
		$this->form_validation->set_rules('description', 'lang:form_description', 'max_length[200]|xss_clean');
		$data['title']		=	'Thêm Banner';
		$this->form_validation->set_error_delimiters('', '<br />');
		
		$config['upload_path'] = './uploads/banner';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '500';
		$config['max_width'] = '1024';
		$config['max_height'] = '768';
		$config['encrypt_name'] = TRUE;
		$config['remove_spaces'] = TRUE;
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		
		if ($this->form_validation->run() == TRUE)
		{
			if ( ! $this->upload->do_upload())
			{
				$this->session->set_flashdata('error', $this->upload->display_errors());
				redirect($this->uri->uri_string());
			}
			else
			{
				$dataimage =  $this->upload->data();	
				$this->banner->add_banner($dataimage);
				$this->session->set_flashdata('message', lang('successfully_created'));
				redirect('admin/website/banner', 'refresh');
			}
			
		}
		
		$this->_template['page']	= "website/banner/add";
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	public function edit($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
		$data['title']		=	'Sửa Banner';
		$lang_working = load_language_working();
		$this->form_validation->set_rules('name', 'lang:form_title', 'required|max_length[60]|xss_clean');
		$this->form_validation->set_rules('url', 'lang:form_url', 'required|max_length[60]|xss_clean');
		$this->form_validation->set_rules('description', 'lang:form_description', 'max_length[200]|xss_clean');

		$this->form_validation->set_error_delimiters('', '<br />');
			
		
		//$config['file_name'] = strtolower('avatar_'.$id);
		$config['upload_path'] = './uploads/banners';
		$config['allowed_types'] = 'gif|jpg|png';
		$config['max_size'] = '500';
		$config['max_width'] = '1024';
		$config['max_height'] = '768';
		//$config['encrypt_name'] = TRUE;
		$config['remove_spaces'] = TRUE;
		$this->load->library('upload', $config);
		$this->upload->initialize($config);
		$data['banners'] = $this->banner->get_banner($id);
		//$old_image	=	$data['banners']->image;
		//$photo_path =  $_FILES['userfile']['name'];
		
		if ($this->form_validation->run() == TRUE)
		{
			if($_FILES['userfile']['name']=='')
			{
				$this->banner->edit_banner($id);
				$this->session->set_flashdata('message', lang('successfully_created'));
				redirect('admin/website/banner', 'refresh');
			}
			else
			{
				if ( ! $this->upload->do_upload())
				{
					$this->session->set_flashdata('error', $this->upload->display_errors());
					redirect('admin/website/banner/edit/'.$id, 'refresh');
					//redirect($this->uri->uri_string());
				}
				else
				{
					$dataimage =  $this->upload->data();	
					$this->banner->edit_banner2($id, $dataimage);
					$this->session->set_flashdata('message', lang('successfully_created'));
					redirect('admin/website/banner', 'refresh');
				}
			}
		}
		
		$this->_template['page']	= "website/banner/edit";
		$this->system_library->load($this->_template['page'], $data, TRUE);
	}
	
	public function enable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->banner->change_status_banner($id,1);
		redirect('admin/website/banner', 'refresh');
		
	}
	
	public function disable($id = null)
	{
		if ($id == null)
		{
			$id = $this->input->post('id');
		}
			
		$this->banner->change_status_banner($id,0);
		redirect('admin/website/banner', 'refresh');
	}
	
	public function delete($id)
	{
		$this->banner->delete_banner($id);
		$this->session->set_flashdata('message', lang('successfully_deleted'));

		redirect('admin/website/banner', 'refresh');
	}
}

/* End of file links.php */
/* Location: ./application/modules/admin/controllers/banners.php */